package com.example.restatr.Atcivity.PetHospital;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.PetHospital.Bean.PawsClawsPetVetBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class PetDoctorListAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private RecyclerView rv;
    private TextView bottomText;
    private List<PawsClawsPetVetBean.RowsBean> rowsBeans = new ArrayList<>();
    private LinearLayout loading;
    private int pageNum = 1;
    private int total;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pet_doctor_list_at);

        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        rv = findViewById(R.id.rv);
        bottomText = findViewById(R.id.bottomText);
        loading = findViewById(R.id.loading);
        GetDoctorList();
        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (!recyclerView.canScrollVertically(1) && newState == 0) {
                    if (rowsBeans.size() < total) {
                        pageNum++;
                        GetDoctorList();
                    } else {
                        bottomText.setText("——到底了——");
                    }
                }
            }
        });
        Tools.SetBarTitle(toolbar,barTitle,getIntent().getStringExtra("name"),search,this,false);

    }

    private void GetDoctorList() {
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/pet-hospital/pet-doctor/list?typeId="+getIntent().getStringExtra("id")+"&pageSize=10&pageNum="+ pageNum, MainActivity.sp.getString("token", ""));
            PawsClawsPetVetBean rowsBean = new Gson().fromJson(token, PawsClawsPetVetBean.class);
            total = rowsBean.getTotal();
            rowsBeans.addAll(rowsBean.getRows());
            runOnUiThread(() -> {
                if (rowsBeans.size() > 0) {
                    loading.setVisibility(View.GONE);
                    rv.setAdapter(new adapter());
                    if (rowsBeans.size()==total){
                        bottomText.setText("——到底了——");
                    }
                }
            });
        }).start();
    }

    class adapter extends RecyclerView.Adapter<adapter.viewHolder> {

        @NonNull
        @Override
        public adapter.viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.lawyer_item, parent, false);
            return new adapter.viewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull adapter.viewHolder holder, int position) {
            Glide.with(PetDoctorListAt.this)
                    .load(Tools.ip + rowsBeans.get(position).getAvatar())
                    .transform(new CircleCrop())
                    .into(holder.head);
            holder.name.setText(rowsBeans.get(position).getName());
            holder.workStartAt.setText("职业证号" + rowsBeans.get(position).getPracticeNo());
            holder.serviceTimes.setText("从业时间 " + rowsBeans.get(position).getWorkingYears()+"年");
            holder.favorableRate.setVisibility(View.GONE);
            holder.legalExpertiseName.setText(rowsBeans.get(position).getJobName());
            holder.separate.setVisibility( View.GONE );
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(PetDoctorListAt.this, PetDoctorDetailsAt.class);
                    intent.putExtra("data",new Gson().toJson(rowsBeans.get(position)));
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return rowsBeans.size();
        }

        class viewHolder extends RecyclerView.ViewHolder {
            private ImageView head;
            private TextView name;
            private TextView workStartAt;
            private TextView serviceTimes;
            private TextView favorableRate;
            private TextView legalExpertiseName;
            private CardView cardBtn;
            private View separate;


            public viewHolder(@NonNull View itemView) {
                super(itemView);
                head = itemView.findViewById(R.id.head);
                name = itemView.findViewById(R.id.name);
                separate =  itemView.findViewById(R.id.separate);
                workStartAt =itemView.findViewById(R.id.workStartAt);
                serviceTimes = itemView.findViewById(R.id.serviceTimes);
                favorableRate = itemView.findViewById(R.id.favorableRate);
                legalExpertiseName = itemView.findViewById(R.id.legalExpertiseName);
                cardBtn = itemView.findViewById(R.id.cardBtn);
            }
        }
    }
}